from main.resourceAssignment import Banker

#-----------------------------------------------------------------------#
#------------------CLASS LongTermScheduler------------------------------#
#-----------------------------------------------------------------------#

class LongTermScheduler():
#--------------------------INSTANCIATION--------------------------------#  
    def __init__(self, arrivingProcess, readypQueue ,aKernel ):
        self.arrivingProcessesQueue = arrivingProcess
        self.readyProcessesQueue = readypQueue #cuando inicia esta vacia
        self.kernel = aKernel     
  
    def dispatchAProcess(self,aPhysicalMemory,aDisk):
        candidate = self.arrivingProcessesQueue.pop(0)  
        if(aPhysicalMemory.hasSpace(candidate.sizeOfAProcess())):    
            self.readyProcessesQueue.append(candidate)                                 #Lo agrega a la cola de listos
            self.kernel.interrupt_processArrivedToRPQ(candidate)
            if(aPhysicalMemory.hasSpace(candidate.sizeOfAProcess())):
                aPhysicalMemory.addingPolicy.saveProcessInMemory(candidate,aPhysicalMemory)#Lo agrega en memoria
                aPhysicalMemory.processesTable.append(candidate)                           #Lo agrega a la tabla de procesos en memoria
            else:
                aPhysicalMemory.anOtherwise(candidate, aDisk, self.kernel.cpu)
        else:
            self.arrivingProcessesQueue.append(candidate)
            
class LongTermScheduler_withBanker(LongTermScheduler):
    def __init__(self,arrivingProcess, readypQueue ,aKernel ):
        LongTermScheduler.__init__(self,arrivingProcess, readypQueue ,aKernel )
        self.banker = Banker(self.kernel,self.kernel.cpu.io.devices,self.kernel.memory)
            
    def dispatchAProcess(self,aPhysicalMemory,aDisk):
        if self.banker.isInASafeStateAdding(self.arrivingProcessesQueue.processes[0]):
            super().dispatchAProcess(aPhysicalMemory,aDisk)
        else:
            self.arrivingProcessesQueue.append(self.arrivingProcessesQueue.pop(0))